package com.guli.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.edu.entity.EduTeacher;
import com.guli.edu.entity.fuzzy.TeacherLike;
import com.guli.edu.mapper.EduTeacherMapper;
import com.guli.edu.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author ZhaoZhenyu
 * @since 2022-09-20
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Autowired
    private EduTeacherMapper eduTeacherMapper;

    // 模糊带分页的条件
    @Override
    public void pageQuery(Page<EduTeacher> eduTeacherPage, TeacherLike teacherLike) {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        if (!ObjectUtils.isEmpty(teacherLike)) {
            String nameLike = teacherLike.getNameLike();
            Integer levelLike = teacherLike.getLevelLike();
            String beginLike = teacherLike.getBeginLike();
            String endLike = teacherLike.getEndLike();
            if (StringUtils.hasText(nameLike)) wrapper.like("name", nameLike);
            if (!StringUtils.isEmpty(levelLike)) wrapper.like("level", levelLike);
            if (StringUtils.hasText(beginLike))wrapper.like("gmt_create", beginLike);
            if (StringUtils.hasText(endLike))wrapper.like("gmt_modified", endLike);
        }
        baseMapper.selectPage(eduTeacherPage, wrapper);
    }

    // 添加redis缓存
    @Cacheable(value = "teacher",key = "'getHotTeacher'")
    @Override
    public List<EduTeacher> getHotTeacher() {
        QueryWrapper<EduTeacher> teacherWrapper = new QueryWrapper<>();
        teacherWrapper.orderByDesc("id").last("limit 4");
        return baseMapper.selectList(teacherWrapper);
    }

    @Override
    public Map<String, Object> getPageOfFront(Page<EduTeacher> pageParam) {
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");

        baseMapper.selectPage(pageParam, queryWrapper);

        // 分页信息
        List<EduTeacher> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }
}
